#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>

struct person { std::string name, position; int ability; };
struct pair { std::string s1, s2; int p; };

std::istream & operator>>(std::istream & is, person & a) { is >> a.name >> a.ability >> a.position; return is; }
std::istream & operator>>(std::istream & is, pair & a) { is >> a.s1 >> a.s2 >> a.p; return is; }
bool operator<(person const & a, person const & b) { return a.position < b.position; }

int const n = 23;
int const inf = 1<<25;
int const team_n = 11;
int m, ans = -inf;
int pos_point[5]; // 1:d 2:g 3:m 4:s
char const pos_index[4] = {'d', 'g', 'm', 's'};
int const pos_num[4] = {4, 1, 4, 2};
int profit[n + 4][n + 4];

std::vector<person> da(n);
std::vector<pair> extra;
std::map<std::string, int> index;

bool check_possible()
{
	for (int i = 1; i <= 4; i++) {
		for (pos_point[i] = pos_point[i-1]; pos_point[i] < n &&
				da[pos_point[i]].position[0] == pos_index[i-1]; pos_point[i]++);
		if (pos_point[i] - pos_point[i-1] < pos_num[i-1]) return false;
	}
	return true;
}

bool vis[n + 4];
std::vector<int> team(team_n + 4);

void check()
{
	int sum = 0;
	for (int i = 0; i < team_n; i++) sum += da[team[i]].ability;
	for (int i = 0; i < team_n; i++)
		for (int j = i + 1; j < team_n; j++)
			sum += profit[team[i]][team[j]];
	ans = std::max(ans, sum);
}

std::map<char, int> pos;
void dfs(int index, int deep, std::vector<int> const & s) // 0:d 1:g 2:m 3:s
{
	for (int i = 0; i < 4; i++)
		if ((index >= pos_point[i+1] && s[i] < pos_num[i]) || s[i] > pos_num[i] ||
				(index < pos_point[i+1] && pos_point[i+1] - index + s[i] < pos_num[i])) return;
	if (deep == team_n) { check(); return; }

	for (int i = index; i < n; i++) {
		if (!vis[i]) {
			std::vector<int> ts = s;
			ts[pos[da[i].position[0]]]++;
			vis[i] = true; team[deep] = i;
			dfs(i+1, deep+1, ts);
			vis[i] = false;
		}
	}
}

int main()
{
	std::ios::sync_with_stdio(false);
	pos['d'] = 0; pos['g'] = 1; pos['m'] = 2; pos['s'] = 3;
	while (std::cin >> da[0]) {
		for (int i = 1; i < n; i++) std::cin >> da[i];
		std::cin >> m; extra.resize(m);
		for (int i = 0; i < m; i++) std::cin >> extra[i];

		std::sort(da.begin(), da.end());
		index.clear();
		for (int i = 0; i < n; i++) index[da[i].name] = i;

		for (int i = 0; i < n; i++)
			for (int j = 0; j < n; j++) profit[i][j] = 0;
		for (int i = 0; i < m; i++) {
			int p1 = index[extra[i].s1], p2 = index[extra[i].s2];
			profit[p1][p2] = profit[p2][p1] = extra[i].p;
		}

		if (!check_possible()) { std::cout << "impossible\n"; continue; }

		ans = -inf;
		for (int i = 0; i < n; i++) vis[i] = false;
		std::vector<int> state(4, 0);
		dfs(0, 0, state);
		std::cout << ans << '\n';
	}
}

